use tauri::command;
use crate::types::ApiResponse;
use diff::lines;
use diff::Result as DiffResult;

#[command]
pub fn compute_text_diff(text1: String, text2: String) -> ApiResponse {
    let t1 = text1.as_str();
    let t2 = text2.as_str();

    // 计算差异
    let differences = lines(t1, t2);

    // 生成带高亮的文本
    let mut text1_result = String::new();
    let mut text2_result = String::new();

    for part in differences {
        match part {
            DiffResult::Left(l) => {
                text1_result.push_str(&format!("<span class=\"removed\">{}</span><br>", l));
            }
            DiffResult::Right(r) => {
                text2_result.push_str(&format!("<span class=\"added\">{}</span><br>", r));
            }
            DiffResult::Both(l, _) => {
                let value = l
                    .replace(' ', "&#x2007;")
                    .replace('\t', "&#x2007;&#x2007;&#x2007;&#x2007;");
                text1_result.push_str(&format!("<span class=\"unchanged\">{}</span><br>", value));
                text2_result.push_str(&format!("<span class=\"unchanged\">{}</span><br>", value));
            }
        }
    }

    ApiResponse::success(serde_json::json!({
        "text1Result": text1_result,
        "text2Result": text2_result
    }).to_string())
} 